package org.kugua.effect.impl;

import java.awt.*;

import lombok.extern.slf4j.Slf4j;
import org.kugua.context.CaptchaContext;
import org.kugua.effect.IEffect;
import org.kugua.utils.RandomUtils;

/**
 * 圈/点 - 干扰效果
 *
 * @author 苦瓜
 * @create 2024-08-27
 * @since 1.0.0
 */
@Slf4j
public class OvalEffect implements IEffect {

    /**
     * 干扰圈数量
     */
    private Integer ovalNum = 10;

    private boolean isFill = false;

    public void setFill(boolean fill) {
        isFill = fill;
    }

    public void setOvalNum(Integer ovalNum) {
        this.ovalNum = ovalNum;
    }

    /**
     * 圈/点 - 效果
     *
     * @param context 上下文信息
     */
    @Override
    public void effect(CaptchaContext context) {
        log.trace("OvalProcessor Effect Process.");
        Graphics2D graphics = context.getGraphics();
        for (int i = 0; i < ovalNum; i++) {
            Color color = new Color(RandomUtils.random(255),
                    RandomUtils.random(255),
                    RandomUtils.random(255));
            graphics.setColor(color);
            // 半径
            int radius = RandomUtils.random(10) + 5;
            int x = RandomUtils.random(context.getWidth() - radius) + radius;
            int y = RandomUtils.random(context.getHeight() - radius) + radius;
            if (isFill) {
                graphics.fillOval(x, y, radius, radius);
            } else {
                graphics.drawOval(x, y, radius, radius);
            }
        }
    }
}
